package com.zhhs.jzcd.account.service.impl;

import java.util.List;
import java.util.Set;

import com.zhhs.common.utils.DateUtils;
import com.zhhs.common.utils.SecurityUtils;
import com.zhhs.jzcd.account.vo.AmountDetailsVo;
import com.zhhs.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhhs.jzcd.account.mapper.BaseJournalAccountMapper;
import com.zhhs.jzcd.account.domain.BaseJournalAccount;
import com.zhhs.jzcd.account.service.IBaseJournalAccountService;

/**
 * 企业账户流水Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-04
 */
@Service
public class BaseJournalAccountServiceImpl implements IBaseJournalAccountService
{
    @Autowired
    private BaseJournalAccountMapper baseJournalAccountMapper;

    /**
     * 查询企业账户流水
     *
     * @param id 企业账户流水主键
     * @return 企业账户流水
     */
    @Override
    public BaseJournalAccount selectBaseJournalAccountById(Long id)
    {
        return baseJournalAccountMapper.selectBaseJournalAccountById(id);
    }

    /**
     * 查询企业账户流水列表
     *
     * @param baseJournalAccount 企业账户流水
     * @return 企业账户流水
     */
    @Override
    public List<BaseJournalAccount> selectBaseJournalAccountList(BaseJournalAccount baseJournalAccount)
    {
        return baseJournalAccountMapper.selectBaseJournalAccountList(baseJournalAccount);
    }

    /**
     * 新增企业账户流水
     *
     * @param baseJournalAccount 企业账户流水
     * @return 结果
     */
    @Override
    public int insertBaseJournalAccount(BaseJournalAccount baseJournalAccount)
    {
        baseJournalAccount.setCreateTime(DateUtils.getNowDate());
        return baseJournalAccountMapper.insertBaseJournalAccount(baseJournalAccount);
    }

    /**
     * 修改企业账户流水
     *
     * @param baseJournalAccount 企业账户流水
     * @return 结果
     */
    @Override
    public int updateBaseJournalAccount(BaseJournalAccount baseJournalAccount)
    {
        baseJournalAccount.setUpdateTime(DateUtils.getNowDate());
        return baseJournalAccountMapper.updateBaseJournalAccount(baseJournalAccount);
    }

    /**
     * 批量删除企业账户流水
     *
     * @param ids 需要删除的企业账户流水主键
     * @return 结果
     */
    @Override
    public int deleteBaseJournalAccountByIds(Long[] ids)
    {
        return baseJournalAccountMapper.deleteBaseJournalAccountByIds(ids);
    }

    /**
     * 删除企业账户流水信息
     *
     * @param id 企业账户流水主键
     * @return 结果
     */
    @Override
    public int deleteBaseJournalAccountById(Long id)
    {
        return baseJournalAccountMapper.deleteBaseJournalAccountById(id);
    }


    /**
     * 账户流水金额统计
     * @return 结果
     */
    @Override
    public AmountDetailsVo selectAmountDetails(){
            return baseJournalAccountMapper.selectAmountDetails(SecurityUtils.getLoginUser().getUser().getUserId(),
                    isAdminOrRoot());

    }

    /**
     * 判断用户是否为admin角色或root角色
     * @return 结果
     */
    private Boolean isAdminOrRoot(){
        Set<String> permissions = SecurityUtils.getLoginUser().getPermissions();
        return permissions.stream().anyMatch(permission -> "admin".equals(permission) || "root".equals(permission));
    }

    /**
     * 企业名称下拉框
     * @return 结果
     */
    @Override
    public List<SysUser> selectBusinessName(){
        return baseJournalAccountMapper.selectBusinessName();
    }

}
